package com.kefu.robot.controller;

import com.kefu.common.exception.BusinessException;
import com.kefu.robot.model.RobotAidataDocType;
import com.kefu.robot.vo.RobotAidataDocTypeInsertVo;
import com.kefu.sys.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.robot.service.RobotAidataDocTypeService;
import com.kefu.robot.vo.RobotAidataDocTypeSearchVo;
import com.kefu.robot.vo.RobotAidataDocTypeUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * @author xiaoyu
 * @date 2023/11/03
 */
@RestController
@Api(tags = "AI模型词条分类表")
@RequestMapping("robot/aidata/docType")
public class RobotAidataDocTypeController extends AbstractBaseController {

    @Autowired
    private RobotAidataDocTypeService robotAidataDocTypeService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @PostMapping("/add")
    @ApiOperation(value = "新增AI模型词条分类表")
    public Result<String> add(@Validated @RequestBody RobotAidataDocTypeInsertVo robotAidataDocTypeVo) {
        logger.info("新增AI模型词条分类表:{}", robotAidataDocTypeVo);
        if (!robotAidataDocTypeVo.getParentId().equals("0") && robotAidataDocTypeVo.getParentId().length() != 19) {
            throw BusinessException.msg("上级分类不存在");
        }
        RobotAidataDocType info = robotAidataDocTypeVo.convert();
        robotAidataDocTypeService.saveAidataDocType(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除AI模型词条分类表")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除AI模型词条分类表:{}", delete);

        robotAidataDocTypeService.removeAidataDocType(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改AI模型词条分类表")
    public Result<String> update(@Validated @RequestBody RobotAidataDocTypeUpdateVo robotAidataDocTypeVo) {
        logger.info("修改AI模型词条分类表:{}", robotAidataDocTypeVo);

        RobotAidataDocType info = robotAidataDocTypeVo.convert();
        robotAidataDocTypeService.updateById(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "AI模型词条分类表详情")
    public Result<RobotAidataDocType> detail(@Validated @RequestBody DetailVo detail) {

        RobotAidataDocType robotAidataDocType = robotAidataDocTypeService.getById(detail.getId());
        if (StringUtils.isNotBlank(robotAidataDocType.getCreateUser())) {
            String createUserName = sysUserMapper.selectById(robotAidataDocType.getCreateUser()).getUserName();
            robotAidataDocType.setCreateUserName(createUserName);
        }
        if (StringUtils.isNotBlank(robotAidataDocType.getUpdateUser())) {
            String updayeUserName = sysUserMapper.selectById(robotAidataDocType.getUpdateUser()).getUserName();
            robotAidataDocType.setUpdateUserName(updayeUserName);
        }
        return ResultGenerator.genSuccessResult(robotAidataDocType);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询AI模型词条分类表")
    public Result<List<?>> list(@Validated @RequestBody RobotAidataDocTypeSearchVo search) {
        List<?> list = robotAidataDocTypeService.listTree(search);
        return ResultGenerator.genSuccessResult(list);
    }
}
